#ifndef RADIOBLL_H
#define RADIOBLL_H
#include <QObject>
#include <QThread>
#include <QMutex>
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDebug>
#include "EZTallyDictionary.h"
#include "EZTallyArray.h"
#include "EZTallyAutoPtr.h"
#include "http/ezqhttp.h"

class RadioModel;
class RadioPlayListModel;
class SongModel;
class RadioBll;


class RadioBLLDelegate
{
public:
    virtual void onGetSongsFinish(RadioBll*/*bll*/,EZTally::EZTallyArray<SongModel*> */*songs*/,int /*error*/){}
};


class RadioBll : public QObject
{
    Q_OBJECT
public:
    RadioBll(QObject *parent=0);
    ~RadioBll();

public:
    static RadioPlayListModel* playListWithRadio(RadioModel*,bool isGetSong=false);
    static EZTally::EZTallyAutoPtr  ParseRadioFromNetStr(const QByteArray &bytearray,int *error);
    static EZTally::EZTallyAutoPtr ParseRadioSongsFromNetstr(const QByteArray &bytes,int *error,QString **outOffset);

    void getRadios();
    int getSongs(int songCount = 20, const QString &offset = QString(""));



    RadioModel *curRadio() const;
    void setCurRadio(RadioModel *curRadio);

    RadioBLLDelegate *delegate() const;
    void setDelegate(RadioBLLDelegate *delegate);

private:
    EZQHTTPNS::EZQHttp *_songhttp;

    RadioModel *_curRadio;
    RadioBLLDelegate *_delegate;
signals:
    void getRadioFinish_SIG(EZTally::EZTallyArray<RadioModel*> *radios,int error);
    void getSongsFinish_SIG(EZTally::EZTallyAutoPtr ptr,int error);
public slots:
    void getRadioFinish_SLOT(EZTally::EZTallyArray<RadioModel*> *radios,int error);
//    void getSongsFinish_SLOT(QNetworkReply*);
    void getSongFinish_SLOT();

};

class GetRadioThread : public QThread
{
    Q_OBJECT
public:
    QMutex *mutex;
    EZTally::EZTallyArray<RadioModel*> **radios;
    GetRadioThread(QMutex *mutex_,EZTally::EZTallyArray<RadioModel*> **radios_);
    ~GetRadioThread();

signals:
    void rungetRadioFinish(EZTally::EZTallyArray<RadioModel*> *radios,int error);
protected:
    void run();

};

#endif // RADIOBLL_H
